﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BO;
using BrightIdeasSoftware;
using UI.Core;
using UIControls;
namespace UI
{
    public partial class UserReportForm :EditFormBase,IMenuForm
    {
        public UserReportForm()
        {
            InitializeComponent();

            MyGrid = grid;
        }

        protected override object OnAdd()
        {
            var obj = new UserReport();
            return obj;

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        private void UserReportForm_Load(object sender, EventArgs e)
        {
            OnSetGridColumns();
            InitGrid();
            OnQuery();
            MyGrid.RowObjectType = typeof(UserReport);

            btnModify.Visible = false;
            btnSave.Enabled = false;
        }
        UserReportList mySource;
       
        protected override void FetchData()
        {
            mySource = ListFactory<UserReportList>.GetList("");
          

            GridDataSource = mySource;
    
        }
        protected override void DataBind()
        {
            base.DataBind();
        
        }
        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();
            cols.Add(new OLVColumn("编号", DataBaseObject.Columns.UserReport_ID) { Width = 50, IsEditable=false });
            cols.Add(new OLVColumn("报表名称", DataBaseObject.Columns.UserReport_ReportName) { Width = 250, IsEditable = false });
            
            MyGrid.Columns.AddRange(cols.ToArray());


        }
        override protected void OnExport()
        {
            var dt = reportGrid.GenerateDataTable();
            var excel = new ExcelAdapter();
            excel.SaveExcel(dt, "", reportGrid.LabelText, reportGrid.LabelText);
        }
        protected override void OnSearch()
        {
            this.TimedFilter(reportGrid, SearchText,0);
        }
        protected override void OnSave()
        {
            try
            {
                foreach (UserReport delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                     if (delObj.ID == 0)
                        continue;
                     mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (UserReport obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                     if (obj.ID == 0)
                         mySource.Add(obj);
               
                }
                mySource.Save();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }      
        private void btnModify_Click(object sender, EventArgs e)
        {
            var obj = MyGrid.SelectedObject as UserReport;
            if (obj == null)
                return;
            var form = new UserReportEditForm(obj);
            form.ShowDialog();
            MyGrid.RefreshObject(obj);
        }

        private void btnView_Click(object sender, EventArgs e)
        {

            var obj = MyGrid.SelectedObject as UserReport;
            if (obj == null)
                return;

           Cursor = Cursors.WaitCursor;
            btnView.Enabled = false;

            try
            {
                var dt = BinDataTableAdapter.GetDataTable(obj.ReportSql);
                reportGrid.SuspendLayout();
                reportGrid.LabelText = obj.ReportName;
                reportGrid.SetObjects(dt);
                reportGrid.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                reportGrid.ResumeLayout();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcptionString("查询语句有错误,请修改后再行查询.");
            }
            finally
            {
                Cursor = Cursors.Default;
                btnView.Enabled = true;
            }
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            //if (UndoList.Count == 0)
            //{
            //    var r = MessageBox.Show("是否保存所做的修改", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            //    if (r == System.Windows.Forms.DialogResult.Yes)
            //    {
            //        UndoList.Clear();
            //        RedoList.Clear();
            //        OnSave();
            //    }
            //}
            //else
            //    base.OnClosing(e);
          
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            btnModify.Visible = true;
            btnSave.Enabled = true;
        }
    }
}
